#!/usr/bin/env python
# coding=utf-8


'''
完结漫画列表获得
https://www.kuimh.com/booklist?end=1
'''

import requests
import parsel
import re


# 过滤封面图片地址
def getpic(p):
    pattern = re.compile(r'\((.*?)\)', re.S)
    r = pattern.findall(p)
    return r


def mhlists():
    url = 'https://www.kuimh.com/booklist?end=1'

    response = requests.get(url)
    data_html = response.text

    selector = parsel.Selector(data_html)

    lstitle = selector.css('.mh-list.col7 .mh-item')

    for item in lstitle:
        title = item.css('.mh-item-detali a::attr(title)').extract()[0]  # 标题
        href = item.css('.mh-item-detali a::attr(href)').extract()[1]  # 正文连接
        pic = item.css('.mh-item p::attr(style)').extract()[0]  # 封面图片
        ttt = getpic(pic)[0]  # 过滤后的封面图片地址
        print(title, href, ttt)

        # 图片按路径地址筛选后存入本地文件夹
        image = requests.get(ttt).content
        with open(r'../images/' + href[6:] + '.jpg', mode='wb') as f:
            f.write(image)


if __name__ == '__main__':

    mhlists()
